package com.huiyujiaoyu.powertime.ui.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.util.KeyboardUtil;
import com.huiyujiaoyu.util.ObjectUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.huiyujiaoyu.base.AppConfig.HISTORYRECORD;


/**
 * Created by win7 on 2017/6/17.
 * 更多热门文章界面
 */

public class SearchActivity extends BaseActivity {

    @BindView(R.id.icon_search)
    ImageView mIconSearch;
    @BindView(R.id.edt_search)
    EditText mEdtSearch;
    @BindView(R.id.ll_search)
    LinearLayout mLlSearch;
    @BindView(R.id.tv_cancle)
    TextView mTvCancle;
    @BindView(R.id.tv_history)
    TextView mTvHistory;
    @BindView(R.id.icon_delete)
    ImageView mIconDelete;
    @BindView(R.id.rl_history)
    RelativeLayout mRlHistory;
    @BindView(R.id.flow_history)
    TagFlowLayout mFlowHistory;
    @BindView(R.id.ll_flow_history)
    LinearLayout mLlFlowHistory;
    @BindView(R.id.tv_hot)
    TextView mTvHot;
    @BindView(R.id.flow_hot)
    TagFlowLayout mFlowHot;
    @BindView(R.id.ll_hot)
    LinearLayout mLlHot;
    @BindView(R.id.ll_his_hot)
    LinearLayout mLlHisHot;
    @BindView(R.id.ll_all)
    LinearLayout mLlAll;
    private List<IndexItemModel> hotdata;
    private TagAdapter<IndexItemModel> hotAdapter;

    //历史记录
    private List<String> locallist;
    private TagAdapter<String> historyAdapter;
    private LayoutInflater mInflater;

    public void initView() {
        setTitleNoBar();
        mInflater = LayoutInflater.from(mContext);
    }

    public void initData() {
        //历史数据
        locallist = (List<String>) ObjectUtils.readObject(HISTORYRECORD);
        if (locallist == null) {
            locallist = new ArrayList<>();
        }
        historyAdapter = new TagAdapter<String>(locallist) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                TextView tv = (TextView) mInflater.inflate(R.layout.flow_item,
                        mFlowHistory, false);
                tv.setText(o);
                return tv;
            }
        };
        mFlowHistory.setAdapter(historyAdapter);

        //热门数据
        hotdata = new ArrayList<>();
        hotAdapter = new TagAdapter<IndexItemModel>(hotdata) {
            @Override
            public View getView(FlowLayout parent, int position, IndexItemModel o) {
                TextView tv = (TextView) mInflater.inflate(R.layout.flow_item,
                        mFlowHistory, false);
                tv.setText(o.name);
                return tv;
            }
        };
        mFlowHot.setAdapter(hotAdapter);
        HttpMethods.getInstance().hotkeys(mContext, getComp(), this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void onDestroy() {
        if (null != locallist) {
            ObjectUtils.fileSave(this, locallist, HISTORYRECORD);
        }
        super.onDestroy();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mFlowHot.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                SearchResult1Activity.launch(mContext,hotdata.get(position).name);
                return true;
            }
        });
        mFlowHistory.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                SearchResult1Activity.launch(mContext,locallist.get(position));
                return true;
            }
        });
        mEdtSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    // 先隐藏键盘
                    KeyboardUtil.hideSoftKeyboard(SearchActivity.this);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    String key = mEdtSearch.getText() + "";
                    if (!TextUtils.isEmpty(key) ) {
                        if(!locallist.contains(key)){
                            locallist.add(key);
                            historyAdapter.notifyDataChanged();
                        }
                        mEdtSearch.setText("");
                        SearchResult1Activity.launch(mContext,key);
                    }

                }
                return false;
            }
        });
    }


    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_HOTKEYS:
                SimpleListBean bean = (SimpleListBean) o;
                hotdata.addAll(bean.list);
                hotAdapter.notifyDataChanged();
                break;
        }
    }



    @OnClick({R.id.icon_delete,R.id.tv_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.icon_delete:
                locallist.clear();
                historyAdapter.notifyDataChanged();
                break;
            case R.id.tv_cancle:
                finish();
                break;
        }
    }
}
